package com.estylesoft.pshevch.fw;

import com.estylesoft.pshevch.model.MovieData;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.ArrayList;
import java.util.List;

public class MovieHelper extends HelperBase {
    public MovieHelper(ApplicationManager applicationManager) {
        super(applicationManager);
    }

    public void fillMovieForm(MovieData movieData) {
        fillTextField(By.name("imdbid"), movieData.getImdbId());
        fillTextField(By.name("name"), movieData.getName());
        fillTextField(By.name("aka"), movieData.getAka());
        fillTextField(By.name("year"), movieData.getYear());
        fillTextField(By.name("duration"), movieData.getDuration());
        fillTextField(By.name("rating"), movieData.getRating());

        setYesNoRadioButton("own", movieData.isOwnFlag());
        setYesNoRadioButton("seen", movieData.isSeenFlag());
        setYesNoRadioButton("loaned", movieData.isLoanedFlag());

        uploadFile(By.id("cover"), movieData.getCoverPath());

        fillTextField(By.name("trailer"), movieData.getTrailerUrl());
        fillTextField(By.name("notes"), movieData.getNotes());
        fillTextField(By.name("taglines"), movieData.getTaglines());
        fillTextField(By.name("plotoutline"), movieData.getPlotOutline());
        fillTextField(By.name("plots"), movieData.getPlots());
        fillTextField(By.name("languages"), movieData.getLanguages());
        fillTextField(By.name("subtitles"), movieData.getSubtitles());
        fillTextField(By.name("audio"), movieData.getAudio());
        fillTextField(By.name("video"), movieData.getVideo());
        fillTextField(By.name("country"), movieData.getCountry());
        fillTextField(By.name("genres"), movieData.getGenres());
        fillTextField(By.name("director"), movieData.getDirector());
        fillTextField(By.name("writer"), movieData.getWriter());
        fillTextField(By.name("producer"), movieData.getProducer());
        fillTextField(By.name("music"), movieData.getMusic());
        fillTextField(By.name("cast"), movieData.getCast());

    }

    public void removeRandomMovie() {
        clickElement(By.className("movie_box"));

        clickElement(By.cssSelector("img[alt=\"Remove\"]"));
    }

    public void addMovie(MovieData movieData) {
        clickElement(By.cssSelector("img[alt=\"Add movie\"]"));

        fillMovieForm(movieData);

        clickElement(By.id("submit"));
    }

    public String getVerificationMessage(String fieldName) {
        return driver
                .findElement(By.cssSelector("#updateform>table>tbody"))
                .findElement(By.xpath("tr[" + getIndexByElementName(fieldName) + "]"))
                .findElement(By.cssSelector("label.error"))
                .getText();
    }

    public void clearSearchField() {
        driver.findElement(By.id("q")).sendKeys(Keys.CONTROL + "a");
        driver.findElement(By.id("q")).sendKeys(Keys.DELETE);
        driver.findElement(By.id("q")).sendKeys(Keys.RETURN);
    }

    public void searchMovieByName(String searchString) {
        WebElement movieBox = driver.findElement(By.cssSelector("div.movie_box"));
        driver.findElement(By.id("q")).clear();
        driver.findElement(By.id("q")).sendKeys(searchString + Keys.RETURN);
        new WebDriverWait(driver, 10).until(ExpectedConditions.stalenessOf(movieBox));
    }

    public String getMovieListContentText() {
        return getElementText(By.className("content"));
    }

    public String getNonExistentMovieName() {
        List<String> movieNameList = getMovieNameList();
        String searchString = "";
        do{
            searchString = searchString + "a";
        } while (containsPart(movieNameList, searchString));
        return searchString;
    }

    public boolean movieListNotEmpty() {
        return isElementPresent(By.cssSelector("div.title"));
    }

    public String getRandomMovieTitle() {
        return getElementText(By.cssSelector("div.title"));
    }
    public List<String> getMovieNameList() {
        List<String> movieNameList = new ArrayList<String>();

        List<WebElement> movieBoxList = driver.findElement(By.id("results")).findElements(By.className("title"));
        for (WebElement movieBox : movieBoxList){
            movieNameList.add(movieBox.getText());
        }
        return movieNameList;
    }

    public int getIndexByElementName(String elementName) {
        List<WebElement> tableRowList = driver.findElements(By.cssSelector("#updateform>table>tbody>tr"));
        for (WebElement tableRow : tableRowList){
            if (elementName.equals(tableRow.findElement(By.tagName("td")).getText()))
                return tableRowList.indexOf(tableRow) + 1;
        }
        return 0;
    }
}
